/*
 * TestPackage.java  0.4.0 / Oct 25, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.nunit; // NOPMD

import junit.framework.TestSuite;

/**
 * Package Test Helper.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class TestPackage {

// Protected Constructor. ------------------------------------------------------

  /**
   * Constructs new TestPackage object.
   */
  protected TestPackage() {
    // empty constructor body
  }

// Public Static Methods. ------------------------------------------------------

  /**
   * Returns package name for the specified class.
   *
   * @param c the class
   *
   * @return package name for the specified class
   *
   * @throws IllegalArgumentException if the class is <tt>null</tt>
   */
  public static String getPackageName(Class c) {
    if (c == null) {
      throw new IllegalArgumentException("Null class");
    }
    String cn = c.getName();
    return cn.substring(0, cn.lastIndexOf('.'));
  }

  /**
   * Creates test suite.
   *
   * @param name the test suite name
   *
   * @return test suite
   */
  public static TestSuite createTestSuite(String name) {
    return new ExecTestSuite(name);
  }

  /**
   * Creates test suite for the specified package test class.
   *
   * @param c the class
   *
   * @return test suite for the specified package test class
   *
   * @throws IllegalArgumentException if the class is <tt>null</tt>
   */
  public static TestSuite createTestSuite(Class c) {
    return new ExecTestSuite(getPackageName(c));
  }

  /**
   * Starts text test runner.
   *
   * @param c the test class
   * @param args the main method arguments
   */
  public static void startTextTestRunner(Class c, String[] args) {
    if (args != null) {
      int argslen = args.length;
      if (argslen >= 4) { // gcj test helper
        ExecTestSuite.main(new String[] { args[argslen - 4], args[argslen - 3],
                                          args[argslen - 2],
                                          args[argslen - 1] });
        return;
      }
    }
    if (c == null) {
      System.err.println("Null test class"); // NOPMD
    } else {
      junit.textui.TestRunner.main(new String[] {c.getName()});
    }
  }

}
